package controllers

import (
	"fmt"
	"portstoragechanges/models"
	"portstoragechanges/pkg/app"
	"portstoragechanges/pkg/e"
	"portstoragechanges/services"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func Getfc(c *gin.Context) {
	err, data := services.Getfc()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}
	app.OK(c, gin.H{"value": data}, "查询成功")
}

func Delfc(c *gin.Context) {

	data := c.Query("fc_id")
	fmt.Println(data)
	if data == "-1" {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.Delfc(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "删除成功")
}

func Updatefc(c *gin.Context) {
	var data models.Fcreceive
	var datanew models.Fc
	c.ShouldBindJSON(&data)
	datanew.Fc_id = com.StrTo(data.Fc_id).MustInt()
	datanew.Fc_name = data.Fc_name
	datanew.Fc_address = data.Fc_address
	datanew.Fc_phone = data.Fc_phone
	err := services.Updatefc(datanew)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": datanew}, "更新成功！")
}

func Addfc(c *gin.Context) {
	var data models.Fcreceive
	var datanew models.Fc
	err := c.ShouldBindJSON(&data)
	if err != nil {
		panic(err)
	}
	datanew.Fc_id = com.StrTo(data.Fc_id).MustInt()
	datanew.Fc_name = data.Fc_name
	datanew.Fc_address = data.Fc_address
	datanew.Fc_phone = data.Fc_phone
	err = services.Addfc(datanew)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{}, "添加成功！")
}
